﻿using AntdUI;
using AttendanceSysytem.Database.BusinessLogic;
using AttendanceSysytem.Models;
using NPOI.POIFS.Crypt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace AttendanceSysytem.Controls
{
    public partial class ScheduleTablePage : UserControl
    {
        public ScheduleTablePage()
        {
            InitializeComponent();
            calendarEx1.DateChanged += CalendarEx1_DateChanged;


            ListenWorkState();
            SelectDate = calendarEx1.Value;

            rdOffice.CheckedChanged += PepoleType_CheckedChanged;
            rdWorkshop.CheckedChanged += PepoleType_CheckedChanged;


            InitData();

        }

        WorkScheduling_Logic logic = new WorkScheduling_Logic();

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData()
        {

            DateTime dateTime = DateTime.Now.AddYears(-1);
            logic.DbBaseClient.GetSimpleClient<WorkScheduling>().Delete(a => a.Date <= dateTime); //清理一年前的缓存数据

            var lst = logic.AllList();
            var officeSchedulings = lst.FindAll(a => a.EmployeeType == 1);
            var workshopSchedulings = lst.FindAll(a => a.EmployeeType == 2);

            InitDicSchedulings(officeSchedulings, officeDateSets);
            InitDicSchedulings(workshopSchedulings,workshopDateSets);
            reflushBageInfo();
        }

        private void InitDicSchedulings(List<WorkScheduling> officeSchedulings, Dictionary<string, DateBadgeEx> sets)
        {
            foreach (var item in officeSchedulings)
            {
                string BageText = item.Status == 1 ? "班" : item.Status == 2 ? "休" : "";
                Color fillColor = item.Status == 1 ? Color.Black : Color.Red;
                if (!sets.ContainsKey(item.DateKey))
                {
                    sets.Add(item.DateKey, new DateBadgeEx(item.DateKey, BageText, fillColor));
                }
                else
                {
                    sets[item.DateKey] = new DateBadgeEx(item.DateKey, BageText, fillColor);
                }
            }
        }

        /// <summary>
        /// 人员类型变更触发刷新UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void PepoleType_CheckedChanged(object sender, BoolEventArgs e)
        {
            ResetWorkState();

            reflushBageInfo();
        }

        private void ListenWorkState()
        {
            rdWork.CheckedChanged += RdWork_CheckedChanged;
            rdRest.CheckedChanged += RdWork_CheckedChanged;
        }

        private void UnListenWorkState()
        {
            rdWork.CheckedChanged -= RdWork_CheckedChanged;
            rdRest.CheckedChanged -= RdWork_CheckedChanged;
        }


        /// <summary>
        /// 车间日期班次项
        /// </summary>
        Dictionary<string, DateBadgeEx> workshopDateSets = new Dictionary<string, DateBadgeEx>();
        /// <summary>
        /// 办公室班次选择
        /// </summary>
        Dictionary<string, DateBadgeEx> officeDateSets = new Dictionary<string, DateBadgeEx>();



        public DateTime SelectDate {  get; set; }

        private void RdWork_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
           
            if (rdWork.Checked || rdRest.Checked)
            {
                reflushBageInfo();
            }
        }

        public void reflushBageInfo()
        {
            
            calendarEx1.BadgeAction = BadgeAction;
            calendarEx1.LoadBadge();
        }

        private List<DateBadgeEx> BadgeAction(DateTime[] arg)
        {

            int state = 0; // 上班 1 休息2  0 无状态
            if(rdWork.Checked)
                state = 1;
            else if(rdRest.Checked)
                state = 2;

           var EmployeeType = rdOffice.Checked ? 1 : 2;
            string BageText = state == 1 ? "班" : state == 2 ? "休" : "";

            Color fillColor = state == 1 ? Color.Black : Color.Red;

           string key = SelectDate.ToString("yyyy-MM-dd");


            WorkScheduling workScheduling = null;
            if (rdOffice.Checked) // 处理办公室逻辑
            {
                return CheckAndSaveBage(officeDateSets, state, EmployeeType, BageText, fillColor, key, ref workScheduling);
            
            }
            if (rdWorkshop.Checked)
            {
                return CheckAndSaveBage(workshopDateSets, state, EmployeeType, BageText, fillColor, key, ref workScheduling);

            }

            return new List<DateBadgeEx>();
        }

        private List<DateBadgeEx> CheckAndSaveBage(Dictionary<string, DateBadgeEx> sets, int state, int EmployeeType, string BageText, Color fillColor, string key, ref WorkScheduling workScheduling)
        {
            if (state == 0)
                return sets.Values.ToList();




            if (!sets.ContainsKey(key))
            {
                workScheduling = new WorkScheduling()
                {
                    Date = SelectDate,
                    DateKey = key,
                    EmployeeType = EmployeeType,
                    Status = state
                };
                logic.Add(workScheduling);

                sets.Add(key, new DateBadgeEx(key, BageText, fillColor));
            }
            else
            {

                try
                {
                   var client = logic.DbBaseClient.GetSimpleClient<WorkScheduling>();
                    workScheduling = client.GetSingle(a => a.DateKey == key && a.EmployeeType == EmployeeType);
                    workScheduling.Status = state;
                    logic.Update(workScheduling);
                    
                    sets[key] = new DateBadgeEx(key, BageText, fillColor);
                }
                catch (Exception ex)
                {

                }
            }
            return sets.Values.ToList();
        }

        private void CalendarEx1_DateChanged(object sender, AntdUI.DateTimeEventArgs e)
        {
            SelectDate = e.Value;

            ResetWorkState();


        }

        public void ResetWorkState()
        {
            //复位默认值先取消注册事件
            UnListenWorkState();
            rdWork.Checked = rdRest.Checked = false;
            ListenWorkState();
        }
    }
}
